<template>
  <el-card shadow="never" class="margin-top-20 margin-left-20 margin-right-20">
    <!--  -->
    <el-row :gutter="20">
      <!-- 添加按钮  -->
      <el-col :span="6">
        <el-button size="medium" @click="handleClick({})">添加用户</el-button>
        <i class="el-icon-refresh margin-left-10 cursor-pointer" @click="getData" />
      </el-col>
    </el-row>
    <!-- 列表属性及操作 -->

    <el-table
      class="margin-top-10"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="用户名" min-width="150">
        <template slot-scope="scope">
          <div class="l-flex align-items-center padding-top-10">
            <img
              :src="scope.row.avatar"
              class="width-32 height-32 margin-right-10"
              alt=""
            >
            {{ scope.row.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="登录账号" prop="userName" min-width="180" />
      <el-table-column label="手机号" prop="mobile" min-width="130" />
      <el-table-column label="邮箱" prop="eMail" min-width="200" />
      <el-table-column label="性别" min-width="50" prop="gender">
        <template slot-scope="{row}" min-width="50">
          <span>{{ row.gender == 1 ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 1" size="small">正常</el-tag>
          <el-tag v-else-if="scope.row.state == 2" size="small" type="warning">
            冻结
          </el-tag>
          <el-tag v-else-if="scope.row.state == 3" size="small" type="danger">
            停用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="200" prop="c_time" />
      <el-table-column fixed="right" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClick(scope.row)">
            编辑
          </el-button>
          <el-divider direction="vertical" />

          <el-button type="text" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="margin-top-10 text-align-right"
      background
      layout="prev, pager, next"
      :current-page.sync="page"
      :total="total"
      @current-change="currentChange"
    />

    <el-drawer
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="false"
    >
      <div class="drawer-title">
        <span>{{ title }}</span>
        <i class="el-icon-close cursor-pointer" @click="drawer = false" />
      </div>
      <div class="drawer-content">
        <!-- 编辑和创建 -->
        <el-row
          v-if="itemUser.id"
          class="border-bottom-eee padding-bottom-20 padding-top-20"
        >
          <el-col :span="24">
            <div class="l-flex align-items-center padding-left-20">
              <img
                :src="itemUser.avatar"
                style="width: 100px; height: 100px"
                alt=""
              >
              <div class="padding-left-20">
                <p>ID：{{ itemUser.id }}</p>
                <p>账号：{{ itemUser.userName }}</p>
                <p>创建时间：{{ itemUser.c_time }}</p>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row class="padding-bottom-20">
          <el-col v-if="!itemUser.id" :span="24">
            <div class="padding-left-16 text-14 padding-top-10">账号</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-input v-model="itemUser.userName" size="small" />
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 text-14 padding-top-10">用户名</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-input v-model="itemUser.name" size="small" />
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 padding-top-10 text-14">手机号</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-input v-model="itemUser.mobile" size="small" />
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 text-14 padding-top-10">邮箱</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-input v-model="itemUser.eMail" size="small" />
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 padding-top-10 text-14">性别</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-select
                v-model="itemUser.gender"
                size="small"
                class="width-100"
              >
                <el-option label="男" :value="1" />
                <el-option label="女" :value="2">女</el-option>
              </el-select>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 text-14 padding-top-10">状态</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-select
                v-model="itemUser.state"
                size="small"
                class="width-100"
              >
                <el-option label="正常" :value="1" />
                <el-option label="冻结" :value="2" />
                <el-option label="关闭" :value="3" />
              </el-select>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="padding-left-16 padding-top-10 text-14">角色分配</div>
            <div class="padding-left-16 padding-right-16 padding-top-10">
              <el-select
                v-model="itemUser.roles"
                size="small"
                class="width-100"
                multiple
              >
                <el-option
                  v-for="(item, index) in roles"
                  :key="index"
                  :label="item.title"
                  :value="item.lable"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="drawer-botton">
        <el-button type="primary" size="small" @click="handleEdit(itemUser)">
          确定
        </el-button>
      </div>
    </el-drawer>
  </el-card>
</template>
<script>
import { getList, deleteUser, create, getRoles } from '@/api/user'
import Table from '_c/Table'
export default {
  components: {
    Table
  },
  data() {
    return {
      page: 1,
      title: '编辑用户',
      drawer: false,
      itemUser: {},
      total: 0,
      tableData: [],
      multipleSelection: [],
      roles: []
    }
  },

  mounted() {
    console.log('mounted')
    this.getData()
    this.getRoles()
  },
  methods: {
    getRoles() {
      getRoles({ limit: 999 }).then((res) => {
        this.roles = res.data.lists
      })
    },
    handleEdit(row) {
      create(row).then(() => {
        this.drawer = false
        this.getData()
      })
    },
    handleClick(row) {
      this.drawer = true
      this.itemUser = this.$deepClone(row)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    currentChange() {
      this.getData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteUser(row)
            .then(() => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getData()
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '删除失败'
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    getData() {
      const data = {
        page: this.page
      }
      getList(data).then((res) => {
        this.total = res.data.total
        this.tableData = res.data.lists
      })
    }
  }
}
</script>
